package com.gxa.hualian.exceptionHandler;

import com.gxa.hualian.exception.BusinessException;
import com.gxa.hualian.utils.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@ControllerAdvice
public class ExceptionController {
    /**
     * 也可以选择将所有异常集中处理
     * 异常的信息是通过声明参数的形式,让SpringMVC自动注入
      * @param e
     * @return
     */
   @ExceptionHandler(Throwable.class)
    public Map exceptionHandler(Exception e){
       JsonResult jsonResult =new JsonResult();
       if(e instanceof BusinessException){
           BusinessException be =((BusinessException) e);
            jsonResult.setCode(be.getExceptionCode().getCode());

       jsonResult.setMsg(be.getExceptionCode().getMessage());

       }else {
           jsonResult.setCode("500");
           jsonResult.setMsg("服务器开小差了");

       }
       return jsonResult.getMap();
    }

}
